package top.swimmer.concurrent.thread;

/**
 * 线程基本状态
 */
public class MyThread {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = go();
        System.out.println("0000-----------" + thread.getState()); // NEW
        thread.start();
        System.out.println("1111-----------" + thread.getState()); // RUNNABLE

        for (;;) {
            if (!thread.isAlive()) {
                System.out.println("3333-----------" + thread.getState()); // TERMINATED
                break;
            }

            System.out.println("2222-----------" + thread.getState());
            Thread.sleep(1000L);
        }
    }

    private static Thread go() {
        return new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i< 2000000; i++) {
                    if (i % 1000000 == 0) {
                        try {
                            // 线程状态 --> TIMED_WAITING
                            Thread.sleep(8000L);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "myThread-test");
    }
}
